package Leo.redis;

import Leo.entity.table.Systemsetting;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class RedisUtil {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    ObjectMapper objectMapper;


    public void save(String key,Object value){
//        if(value instanceof String){
//            value = ((String) value).getBytes();
//        }
        redisTemplate.opsForValue().set(key,value);
        System.out.println("key has wirten in reids by redisTemplate");
        stringRedisTemplate.opsForValue().set(key,value.toString());
        System.out.println("key has wirten in reids by StringredisTemplate");
    }

    public Object getFromRedis(String key){
        Object o = redisTemplate.opsForValue().get(key);
        if(o instanceof String){
            System.out.println(o);
        }
        return o;
    }

    public Object getFromStringRedis(String key){
        String s = stringRedisTemplate.opsForValue().get(key);
        return s;
    }

    public Object putByStringRedis(Long id) throws JsonProcessingException {
        Systemsetting systemsetting = new Systemsetting();
        systemsetting.setSettingId(id);
        systemsetting.setSettingValue("nicai阿里巴巴");
        String s = objectMapper.writeValueAsString(systemsetting);
//        String s = JSON.toJSONString(systemsetting);

        stringRedisTemplate.opsForHash().put("hosp",id +"",s);
        return systemsetting;
    }

    public Object getByStringRedis(Long id){
        Object hosp = stringRedisTemplate.opsForHash().get("hosp", id +"");
        if(hosp != null && hosp instanceof Systemsetting)
            return (Systemsetting)hosp;
        if(hosp instanceof String) {
            try {
                return objectMapper.readValue((String)hosp,Systemsetting.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
//            return JSON.parseObject((String) hosp, Systemsetting.class);
        }
        return "back";
    }

    public Object putByRedis(Long id){
        Systemsetting systemsetting = new Systemsetting();
        systemsetting.setSettingId(id);
        systemsetting.setSettingValue("nicai");
        redisTemplate.opsForHash().put("hosp",id,systemsetting);
        return systemsetting;
    }

    public Object getByRedis(Long id){
        Object hosp = redisTemplate.opsForHash().get("hosp", id);
        if(hosp != null && hosp instanceof Systemsetting)
            return (Systemsetting)hosp;
        return "back";
    }
//    @PostConstruct
//    public void init(){
//        RedisAutoConfiguration redisAutoConfiguration = new RedisAutoConfiguration();
//
//    }


}
